﻿
Imports osi.root.constants
Imports osi.root.delegates
Imports osi.root.utils
Imports osi.root.connector
Imports osi.root.formation
Imports osi.root.lock

Public Module _lifetime_event_comb
    Public Function lifetime_event_comb(ByVal stop_result As Boolean,
                                        ByVal stopping As Func(Of Boolean),
                                        ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        assert(Not stopping Is Nothing)
        Dim d2() As Func(Of Boolean) = Nothing
        ReDim d2(array_size(d) - 1)
        For i As Int32 = 0 To array_size(d) - 1
            Dim j As Int32 = 0
            j = i
            d2(i) = Function() As Boolean
                        If stopping() Then
                            Return stop_result AndAlso
                                   goto_end()
                        Else
                            Return d(j)()
                        End If
                    End Function
        Next
        Return New event_comb(d2)
    End Function

    Public Function success_lifetime_event_comb(ByVal stopping As Func(Of Boolean),
                                                ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        Return lifetime_event_comb(True, stopping, d)
    End Function

    Public Function lifetime_event_comb(ByVal stopping As Func(Of Boolean),
                                        ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        Return lifetime_event_comb(False, stopping, d)
    End Function

    Public Function lifetime_event_comb(ByVal stop_result As Boolean,
                                        ByVal lifetime As wrapper(Of singleentry),
                                        ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        assert(Not lifetime Is Nothing)
        Return lifetime_event_comb(stop_result, Function() Not lifetime Is Nothing AndAlso lifetime.in_use(), d)
    End Function

    Public Function success_lifetime_event_comb(ByVal lifetime As wrapper(Of singleentry),
                                                ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        Return lifetime_event_comb(True, lifetime, d)
    End Function

    Public Function lifetime_event_comb(ByVal lifetime As wrapper(Of singleentry),
                                        ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        Return lifetime_event_comb(False, lifetime, d)
    End Function

    Public Function lifetime_event_comb(ByVal stop_result As Boolean,
                                        ByVal lifetime_ms As Int64,
                                        ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        Dim start_ms As Int64 = 0
        Return lifetime_event_comb(stop_result,
                                   If(If(lifetime_ms >= 0, eva(start_ms, nowadays.milliseconds()), False),
                                      Function() As Boolean
                                          Return nowadays.milliseconds() - start_ms > lifetime_ms
                                      End Function,
                                      Function() False),
                                   d)
    End Function

    Public Function success_lifetime_event_comb(ByVal lifetime_ms As Int64,
                                                ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        Return lifetime_event_comb(True, lifetime_ms, d)
    End Function

    Public Function lifetime_event_comb(ByVal lifetime_ms As Int64,
                                        ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        Return lifetime_event_comb(False, lifetime_ms, d)
    End Function

    Public Function application_lifetime_event_comb(ByVal ParamArray d() As Func(Of Boolean)) As event_comb
        Return lifetime_event_comb(True, Function() application_lifetime.stopping(), d)
    End Function
End Module
